$(function() {
	admin.category.init();
});
Namespace('admin.category',{
	init:function() {
		$('#tree').tree({
			dnd:true,
			animate:true,
			onClick:function(node){
				$('#tree').tree('toggle', node.target);
			},
			onDblClick:function(node) {
				admin.category.update();
			},
			onSelect:function(node) {
				admin.category.onSelect(node);
			},
			onContextMenu: function(e,node) {
				admin.category.onContextMenu(e,node);
			}/*,
			onBeforeDrop: function(target,source,point) {
				return admin.category.onBeforeDrop(target,source,point);
			},
			onDrop: function(target,source,point) {
				admin.category.onDrop(target,source,point);
			}*/
		});
		easyui.util.initTabsHeaderMenu('#tabs');
		$(window).resize(function() {
			$('div.easyui-layout').layout('resize');
		});
	},

	/**
	 * 选中树节点时,执行此方法
	 * @param node
	 */
	onSelect:function(node) {
		
	},
	
	/**
	 * 在树节点上点右键时,执行此方法
	 * @param e
	 * @param node
	 */
	onContextMenu:function(e, node) {
		
		e.preventDefault();		//取消默认动作
		$('#tree').tree('select', node.target);
		
		$('#tree-menu').menu('show',{
			left: e.pageX,
			top: e.pageY
		});
	},
	
	/**
	 * 移动树节点之前,执行此方法
	 * @param target
	 * @param source
	 * @param point
	 */
	onBeforeDrop:function(target,source,point) {
		/*var targetNode=$('#tree').tree('getNode',target);
		if(targetNode.attributes.type==2 && (point==undefined || point=='append')) {
			top.showInfo('移动菜单失败!');
			return false;
		}
		
		var t={
			top:'上面',
			bottom:'下面',
			append:'里面',
			undefined:'里面'
		};
		var msg='确定要将"'+source.text+'"移动到"'+targetNode.text+'"'+t[point]+'吗?';
		return confirm(msg);*/
	},
	/**
	 * 移动树节点后,执行此方法
	 * @param target
	 * @param source
	 * @param point
	 * @returns {Boolean}
	 */
	onDrop:function(target,source,point) {
		
		/*var targetNode=$('#tree').tree('getNode',target);
		$.post(contextPath+'/sys/menu/move',{
			targetId:targetNode.id,
			sourceId:source.id,
			point:point
		},function(result) {
			if(result.code==0) {
				if(point=='append') {
					admin.category.refreshNode(targetNode.id);
				} else {
					admin.category.refreshNode(targetNode.attributes.parentId);
				}
			} else {
				top.showInfo('移动菜单失败!');
				admin.category.refreshNode();
			}
		},'json');*/
	},
	
	/**
	 * 显示根节点的菜单(根节点是个虚节点,实际不存在)
	 * @param e
	 * @returns {Boolean}
	 */
	showRootMenu:function(e) {
		$('#root-menu').menu('show',{
			left: e.pageX,
			top: e.pageY
		});
		return false;
	},
	
	/**
	 * 添加
	 */
	add:function() {
		var node=$('#tree').tree('getSelected');
		if(node==null) {
			top.showInfo('请选择一条记录!');
			return;
		}
		var url=contextPath+'/mall/category/add?parentId='+node.id;
		easyui.util.addTab('#tabs','tab_add','添加分类',url);
	},
	
	/**
	 * 添加一级分类
	 */
	addTop:function() {
		var url=contextPath+'/mall/category/add';
		easyui.util.addTab('#tabs','tab_add','添加分类',url);
	},
	
	/**
	 * 修改分类
	 */
	update:function() {
		var node=$('#tree').tree('getSelected');
		if(node==null) {
			top.showInfo('请选择一条记录!');
			return;
		}
		var url=contextPath+'/mall/category/update/'+node.id;
		easyui.util.addTab('#tabs','tab_'+node.id,'分类：'+node.text,url);
	},
	
	/**
	 * 删除分类
	 */
	remove:function(msg) {
		
		/*var node=$('#tree').tree('getSelected');
		if(node==null) {
			top.showInfo('请选择一条记录!');
			return;
		}
		
		$.messager.confirm('提示信息', '确定要删除吗?', function(sure) {
			if (!sure) return;
			$.post(contextPath+'/sys/menu/delete/'+node.id,function(result) {
				if(result.code==1) {
					admin.menu.refreshNode(node.attributes.parentId);
					$('#tabs').tabs('close','菜单：'+node.text);
				} else {
					top.showInfo('删除菜单失败!');
				}
			},'json');
		});*/
	},
	
	/**
	 * 刷新树节点
	 * @param {Object} treeNodeId
	 */
	refreshNode:function(treeNodeId) {
		if(treeNodeId==null || treeNodeId=='') {
			$('#tree').tree('reload');
		} else {
			var node=$('#tree').tree('find',treeNodeId);
			$('#tree').tree('reload',node.target);
		}
		admin.category.hideTreeToolbar();
	},

	/**
	 * 隐藏树面板的工具栏按钮
	 */
	hideTreeToolbar:function() {
		$('a[group=leaf]').hide();
	},
	
	/**
	 * 修改当前tab页的标题
	 * @param title
	 */
	updateTabTitle:function(title) {
		easyui.util.updateCurrentTabTitle('#tabs',title);
	},
	doAdd:function() {
		var logoImg = "";
		var logoImglist = $(".logo_img img");
		if (logoImglist && logoImglist.length > 0) {
			logoImglist.each(function(){
			    if (logoImg == "") {
			    	logoImg += $(this).attr("src").replace(imgFilePath,"");
			    } else {
			    	logoImg += ";" + $(this).attr("src").replace(imgFilePath,"");
			    }
			});
		}
		var adImg = "";
		var adImglist = $(".ad_img img");
		if (adImglist && adImglist.length > 0) {
			adImglist.each(function(){
			    if (adImg == "") {
			    	adImg += $(this).attr("src").replace(imgFilePath,"");
			    } else {
			    	logadImgoImg += ";" + $(this).attr("src").replace(imgFilePath,"");
			    }
			});
		}
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		data.img = logoImg;
		data.adImg = adImg;
		$.post(contextPath+'/mall/category/add',data,function(result) {
			if(result.code==1) {
				parent.admin.category.refreshNode(data.parentId);
				$('#data-form')[0].reset();
			} else {
				top.showInfo('添加分类失败:'+result.msg);
			}
		},'json');
	},
	doUpdate:function() {
		var logoImg = "";
		var logoImglist = $(".logo_img img");
		if (logoImglist && logoImglist.length > 0) {
			logoImglist.each(function(){
			    if (logoImg == "") {
			    	logoImg += $(this).attr("src").replace(imgFilePath,"");
			    } else {
			    	logoImg += ";" + $(this).attr("src").replace(imgFilePath,"");
			    }
			});
		}
		var adImg = "";
		var adImglist = $(".advertisement_img img");
		if (adImglist && adImglist.length > 0) {
			adImglist.each(function(){
			    if (adImg == "") {
			    	adImg += $(this).attr("src").replace(imgFilePath,"");
			    } else {
			    	logadImgoImg += ";" + $(this).attr("src").replace(imgFilePath,"");
			    }
			});
		}
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		data.img = logoImg;
		data.adImg = adImg;
		$.post(contextPath+'/mall/category/update',data,function(result) {
			if(result.code==1) {
				parent.admin.category.refreshNode(data.parentId);
				parent.admin.category.updateTabTitle('分类：'+data.name);
			} else {
				top.showInfo('修改分类失败:'+result.msg);
			}
		},'json');
	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/mall/category/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
		
	}
});